package com.ciwong.epaper.modules.me.ui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.TimeoutError;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.application.EpaperConstant;
import com.ciwong.epaper.modules.clazz.bean.TeacherClassBean;
import com.ciwong.epaper.modules.epaper.dao.EpaperDao;
import com.ciwong.epaper.modules.me.adapter.BookDeskRecyclerViewAdapter;
import com.ciwong.epaper.modules.me.bean.AdsBean;
import com.ciwong.epaper.modules.me.bean.BookDeskBean;
import com.ciwong.epaper.modules.me.bean.BookmarkBean;
import com.ciwong.epaper.modules.me.bean.BooksPage;
import com.ciwong.epaper.modules.me.bean.EpaperInfoGradeNameBean;
import com.ciwong.epaper.modules.me.bean.FavoritesBook;
import com.ciwong.epaper.modules.me.bean.FavoritesBookPageBean;
import com.ciwong.epaper.modules.me.bean.MaterialBean;
import com.ciwong.epaper.modules.me.bean.RefreshNewBookDesk;
import com.ciwong.epaper.modules.me.bean.ServiceProductsBean;
import com.ciwong.epaper.modules.me.bean.SubjectBean;
import com.ciwong.epaper.modules.me.util.MeJumpManager;
import com.ciwong.epaper.modules.msg.bean.Ad;
import com.ciwong.epaper.ui.BaseFragment;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.epaper.util.DoubleClickCheckUtils;
import com.ciwong.epaper.util.SerializableManager;
import com.ciwong.eventbus.EventBus;
import com.ciwong.libs.utils.NetworkUtils;
import com.ciwong.mobilelib.bean.SchoolDetail;
import com.ciwong.mobilelib.i.BaseCallBack;
import com.ciwong.mobilelib.utils.ContainerType;
import com.ciwong.mobilelib.utils.ToastUtil;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * 新版书桌
 * Created by keepon on 2018/8/6.
 */
public class NewBookDeskFragment extends BaseFragment implements OnRefreshListener, View.OnClickListener {
    private static final String TAG = "NewBookDeskFragment";
    RecyclerView mRecyclerView;
    private TextView mTvGotoResourceCenter;

    private BookDeskBean mBookDeskBean = new BookDeskBean();
    private SwipeToLoadLayout mSwipeToLoadLayout;
    private Button mBtnRetry;
    private List<BookmarkBean> bookmarkBeanList = new ArrayList<>();
    private int brandId = EApplication.BRAND_ID;
    //服务ID
    private int serviceId;

    private int isOwn;
    //班级ID，多个班级ID用“,”号分隔
    private String classIds;
    //学校id
    private int schoolId;
    //学段id
    private int periodId;

    private int[] gradeIds;
    //年级id
    private int gradeId = -1;
    //书籍分类,1教材同步 2课外拓展 -1为所有
    private int bookType = 1;
    //关键字搜索
    private String keyWords;
    //	页码 从1开始
    private int pageIndex = 1;
    //每页条数
    private int pageSize = 49;

    private int mBookmarkId;

    private ServiceProductsBean serviceProductsBean;
    private List<MaterialBean> materialBeanList = new ArrayList<>();
    private SubjectBean subjectBean = new SubjectBean();
    private List<SubjectBean> subjectBeanList = new ArrayList<>();

    private BookDeskRecyclerViewAdapter mBookDeskRecyclerViewAdapter;
    private LinearLayout mLlLoadFailed, mLlNoData;
    private List<EpaperInfoGradeNameBean> epaperInfoGradeNameBeans = new ArrayList<>();

    public static NewBookDeskFragment newInstance() {
        NewBookDeskFragment fragment = new NewBookDeskFragment();
        return fragment;
    }

    public NewBookDeskFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_new_book_desk, container, false);
        findViews(view);
        getMyOneSchool();
        loadLocalHomePageData();
        initEvent();
        init();
        registerBoradcastReceiver();
        EventBus.getDefault().register(this);
        return view;
    }

    private void setLoacdBookMark() {
        bookmarkBeanList.clear();
        BookmarkBean bookmarkBean = new BookmarkBean();
        bookmarkBean.setBookmarkId(3);
        bookmarkBean.setBookmarkName("听说");
        bookmarkBean.setLoaclIcon(R.mipmap.listen_speak_icon);
        bookmarkBeanList.add(bookmarkBean);
        BookmarkBean bookmarkBean2 = new BookmarkBean();
        bookmarkBean2.setBookmarkId(4);
        bookmarkBean2.setBookmarkName("听力");
        bookmarkBean2.setLoaclIcon(R.mipmap.listen_icon);
        bookmarkBeanList.add(bookmarkBean2);
        BookmarkBean bookmarkBean3 = new BookmarkBean();
        bookmarkBean3.setBookmarkId(9);
        bookmarkBean3.setBookmarkName("跟读");
        bookmarkBean3.setLoaclIcon(R.mipmap.follow_read_icon);
        bookmarkBeanList.add(bookmarkBean3);
        BookmarkBean bookmarkBean4 = new BookmarkBean();
        bookmarkBean4.setBookmarkId(7);
        bookmarkBean4.setBookmarkName("笔试");
        bookmarkBean4.setLoaclIcon(R.mipmap.written_examination_icon);
        bookmarkBeanList.add(bookmarkBean4);
        BookmarkBean bookmarkBean5 = new BookmarkBean();
        bookmarkBean5.setBookmarkId(5);
        bookmarkBean5.setBookmarkName("音标");
        bookmarkBean5.setLoaclIcon(R.mipmap.symbol_icon);
        bookmarkBeanList.add(bookmarkBean5);
        BookmarkBean bookmarkBean6 = new BookmarkBean();
        bookmarkBean6.setBookmarkId(1);
        bookmarkBean6.setBookmarkName("典范");
        bookmarkBean6.setLoaclIcon(R.mipmap.dianfan_icon);
        bookmarkBeanList.add(bookmarkBean6);
        BookmarkBean bookmarkBean7 = new BookmarkBean();
        bookmarkBean7.setBookmarkId(2);
        bookmarkBean7.setBookmarkName("阅读");
        bookmarkBean7.setLoaclIcon(R.mipmap.new_concept_icon);
        bookmarkBeanList.add(bookmarkBean7);
        BookmarkBean bookmarkBean8 = new BookmarkBean();
        bookmarkBean8.setBookmarkId(6);
        bookmarkBean8.setBookmarkName("黑布林");
        bookmarkBean8.setLoaclIcon(R.mipmap.black_plum);
        bookmarkBeanList.add(bookmarkBean8);
        BookmarkBean bookmarkBean9 = new BookmarkBean();
        bookmarkBean9.setBookmarkId(8);
        bookmarkBean9.setBookmarkName("趣味");
        bookmarkBean9.setLoaclIcon(R.mipmap.dubbing_icon);
        bookmarkBeanList.add(bookmarkBean9);
        BookmarkBean bookmarkBean10 = new BookmarkBean();
        bookmarkBean10.setBookmarkId(12);
        bookmarkBean10.setBookmarkName("看一看");
        bookmarkBean10.setLoaclIcon(R.mipmap.take_a_look_icon);
        bookmarkBeanList.add(bookmarkBean10);
    }

    private void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(EpaperConstant.BROADCAST_GET_SERVICE_REFRESH_BOOK_DESK);
        //注册广播
        getActivity().registerReceiver(mBroadcastReceiver, myIntentFilter);
    }

    BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent == null) {
                return;
            }
            if (EpaperConstant.BROADCAST_GET_SERVICE_REFRESH_BOOK_DESK.equals(intent.getAction())) {
                //延时一秒去网络请求，不然可能服务器那边没刷新
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        getBookDeskHomePageFromNet();
                    }
                }, 1000);

            }
        }
    };
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
        }
    };

    private void unregisterReceiver() {
        if (mBroadcastReceiver != null)
            getActivity().unregisterReceiver(mBroadcastReceiver);
    }

    private void findViews(View view) {
        mLlNoData = view.findViewById(R.id.ll_no_data);
        mLlLoadFailed = view.findViewById(R.id.ll_load_failed);
        mTvGotoResourceCenter = view.findViewById(R.id.tv_go_to_resource_center);
        mSwipeToLoadLayout = view.findViewById(R.id.swipe_toLoad_layout);
        mBtnRetry = view.findViewById(R.id.btn_retry);
        mRecyclerView = view.findViewById(R.id.swipe_target);

    }

    private void initEvent() {
        mSwipeToLoadLayout.setOnRefreshListener(this);
        mTvGotoResourceCenter.setOnClickListener(this);
        mBtnRetry.setOnClickListener(this);

    }

    private void init() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        mBookDeskRecyclerViewAdapter = new BookDeskRecyclerViewAdapter(mActivity, mBookDeskBean, epaperInfoGradeNameBeans);
        mRecyclerView.setAdapter(mBookDeskRecyclerViewAdapter);
        mRecyclerView.setFocusable(false);
        setLoacdBookMark();
    }

    @Override
    public void onResume() {
        super.onResume();
        //getBookDeskHomePageFromNet();
    }

    public void onEventMainThread(RefreshNewBookDesk refreshNewBookDesk) {
        if (refreshNewBookDesk.isRefreshUnconditional()) {
            getBookDeskHomePageFromNet();
        } else {
            if (mSwipeToLoadLayout.getVisibility() != View.VISIBLE) {
                getBookDeskHomePageFromNet();
            }
        }

    }


    //加载本地的首页数据，包括科目和单元选中情况
    private void loadLocalHomePageData() {
        if (getParentFragment() != null && !getParentFragment().isHidden()) {
            showCricleProgress();
        }
        SerializableManager.getInstance().deSerialize(SerializableManager.SerializeKey.SHARE_KEY_BOOK_DESK_HOME_PAGE, new BaseCallBack() {
            @Override
            public void failed(int errorCode, Object data) {
                failed(data);
            }

            @Override
            public void failed(Object data) {
                getBookDeskHomePageFromNet();
            }

            @Override
            public void success(Object data) {
                try {
                    if (data != null) {
                        hideCricleProgress();
                        mBookDeskBean = (BookDeskBean) data;
                        boolean jiaoCaiTongBuAndKeWaiTuoZhanAllEmpty = isJiaoCaiTongBuAndKeWaiTuoZhanAllEmpty(mBookDeskBean.getSubject(), mBookDeskBean.getBookmark(), mBookDeskBean.getFavoritesBooks(), mBookDeskBean.getAds());
                        if (jiaoCaiTongBuAndKeWaiTuoZhanAllEmpty) {
                            //showContainerType(ContainerType.TYPE_NO_DATA);
                            getBookDeskHomePageFromNet();
                        } else {
                            mBookDeskRecyclerViewAdapter.updateData(mBookDeskBean, true);
                            showContainerType(ContainerType.TYPE_HAVE_DATA);
                        }
                    }
                } catch (Exception e) {

                }

            }
        });
    }

    /**
     * 判断数据是否为空
     *
     * @param subjectBeanList
     * @param bookmarkBeanList
     * @param favoritesBooks
     * @return
     */
    public boolean isJiaoCaiTongBuAndKeWaiTuoZhanAllEmpty(List<SubjectBean> subjectBeanList, List<BookmarkBean> bookmarkBeanList, List<FavoritesBook> favoritesBooks, List<Ad> ads) {
        boolean isJiaoCaiTongbuEmpty = false, isKeWaiTuoZhanEmpty = false, isFavortiesEmpty = false, isAdsEmpty = false;
        //课外拓展没数据隐藏起来
        if (bookmarkBeanList == null || bookmarkBeanList.size() == 0) {
            isJiaoCaiTongbuEmpty = true;
        }
        //教材同步没数据隐藏起来
        if (subjectBeanList == null || subjectBeanList.size() == 0) {
            isKeWaiTuoZhanEmpty = true;
        }
        //收藏列表没数据隐藏起来
        if (favoritesBooks == null || favoritesBooks.size() == 0) {
            isFavortiesEmpty = true;
        }
        //广告没数据隐藏起来
        if (ads == null || ads.size() == 0) {
            isAdsEmpty = true;
        }


        return isJiaoCaiTongbuEmpty && isKeWaiTuoZhanEmpty && isFavortiesEmpty && isAdsEmpty;
    }


    public void getBookDeskHomePageFromNet() {
        getMyClass(String.valueOf(brandId), 2, schoolId);
    }

    private List<TeacherClassBean> teacherClassBeans = new ArrayList<>();

    public void getMyOneSchool() {
        SchoolDetail mySchool = EApplication.getInstance().getMySchool();
        if (null == mySchool) {
            return;
        }
        schoolId = mySchool.getSchoolId();
        periodId = mySchool.getPeriod();
        epaperInfoGradeNameBeans = new ArrayList<>();
        EpaperInfoGradeNameBean epaperInfoGradeNameBean1 = new EpaperInfoGradeNameBean("一年级", 1101);
        EpaperInfoGradeNameBean epaperInfoGradeNameBean2 = new EpaperInfoGradeNameBean("二年级", 1102);
        EpaperInfoGradeNameBean epaperInfoGradeNameBean3 = new EpaperInfoGradeNameBean("三年级", 1103);
        EpaperInfoGradeNameBean epaperInfoGradeNameBean4 = new EpaperInfoGradeNameBean("四年级", 1104);
        EpaperInfoGradeNameBean epaperInfoGradeNameBean5 = new EpaperInfoGradeNameBean("五年级", 1105);
        EpaperInfoGradeNameBean epaperInfoGradeNameBean6 = new EpaperInfoGradeNameBean("六年级", 1106);
        EpaperInfoGradeNameBean epaperInfoGradeNameBean7 = new EpaperInfoGradeNameBean("七年级", 1107);
        EpaperInfoGradeNameBean epaperInfoGradeNameBean8 = new EpaperInfoGradeNameBean("八年级", 1108);
        EpaperInfoGradeNameBean epaperInfoGradeNameBean9 = new EpaperInfoGradeNameBean("九年级", 1109);
        EpaperInfoGradeNameBean epaperInfoGradeNameBean11 = new EpaperInfoGradeNameBean("高一", 1111);
        EpaperInfoGradeNameBean epaperInfoGradeNameBean12 = new EpaperInfoGradeNameBean("高二", 1112);
        EpaperInfoGradeNameBean epaperInfoGradeNameBean13 = new EpaperInfoGradeNameBean("高三", 1113);
        EpaperInfoGradeNameBean epaperInfoGradeNameBean14 = new EpaperInfoGradeNameBean("其他", 333333);
        switch (periodId) {
            case 1:
            case 16:
            case 17:
                break;
            case 2:
            case 3:
            case 18:
            case 19:
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean1);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean2);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean3);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean4);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean5);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean6);
                break;
            case 4:
            case 5:
            case 20:
            case 21:
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean7);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean8);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean9);
                break;
            case 6:
            case 7:
            case 22:
            case 23:
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean1);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean2);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean3);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean4);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean5);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean6);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean7);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean8);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean9);
                break;
            case 8:
            case 9:
            case 24:
            case 25:
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean11);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean12);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean13);
                break;
            case 10:
            case 11:
            case 26:
            case 27:
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean1);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean2);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean3);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean4);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean5);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean6);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean11);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean12);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean13);
                break;
            case 12:
            case 13:
            case 28:
            case 29:
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean7);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean8);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean9);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean11);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean12);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean13);
                break;
            case 14:
            case 15:
            case 30:
            case 31:
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean1);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean2);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean3);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean4);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean5);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean6);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean7);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean8);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean9);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean11);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean12);
                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean13);
                break;
        }
        epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean14);
    }
    /**
     * 获取学校信息
     */
//    public void getMyClass() {
//        getMyClass(String.valueOf(brandId), 2, schoolId);
//    }


    /**
     * 获取班级列表
     */
    public void getMyClass(String brandId, int userRole, int schoolId) {
        EpaperDao.getInstance().getMyClass(brandId, userRole, schoolId, new BaseExtCallBack(getActivity(), EApplication.getInstance().getUserInfoBase().getUserId() + "") {
            @Override
            public void failed(int errorCode, Object data) {
                super.failed(errorCode, data);
                hideCricleProgress();
                if (data instanceof TimeoutError) {
                    ToastUtil.INSTANCE.toastCenterError(getString(R.string.load_faild_check_net_work));
                }
            }

            @Override
            public void failed(Object data) {
                hideCricleProgress();
                if (!NetworkUtils.isOnline()) {
                    ToastUtil.INSTANCE.toastCenterError(getString(R.string.load_faild_check_net_work));
                    return;
                }
            }

            @Override
            public void success(Object data) {
                hideCricleProgress();
                if (null != data && data instanceof ArrayList) {
                    teacherClassBeans = (List<TeacherClassBean>) data;
                    for (int i = 0; i < teacherClassBeans.size(); i++) {
                        if (i == 0) {
                            classIds = String.valueOf(teacherClassBeans.get(i).getClassId());
                        } else {
                            classIds = classIds + "," + teacherClassBeans.get(i).getClassId();
                        }
                    }
                    getServiceProductsByIdClassids();
                }
            }
        });
    }

    /**
     * 获取课外拓展内容
     */
    public void getServiceProductsByIdClassids() {
        EpaperDao.getInstance().getServiceProductsByIdClassids(brandId, isOwn, mBookmarkId, serviceId, "0",
                classIds, schoolId, gradeId, bookType, keyWords, pageIndex, pageSize,
                new BaseExtCallBack(mActivity, EApplication.getInstance().getUserInfoBase().getUserId() + "") {
                    @Override
                    public void success(final Object data) {
                        if (null != data) {
                            serviceProductsBean = (ServiceProductsBean) data;
                            Map<Integer, String> map2 = new LinkedHashMap<>();
                            Map<Integer, String> mapGrade = new LinkedHashMap<>();
                            List<BooksPage> pageList = serviceProductsBean.getPageList();
                            materialBeanList = new ArrayList<>();
                            subjectBeanList = new ArrayList<>();
                            for (int i = 0; i < pageList.size(); i++) {
                                BooksPage item = pageList.get(i);
                                map2.put(item.getProductId(), item.getProductName());
                                mapGrade.put(item.getGrade(), item.getGradeName());
                            }
                            Set<Integer> integers = map2.keySet();
                            for (Integer productId : integers) {
                                MaterialBean materialBean1 = new MaterialBean();
                                List<MaterialBean.BooksBean> beanList = new ArrayList<>();
                                for (int i = 0; i < pageList.size(); i++) {
                                    if (pageList.get(i).getProductId() == productId) {
                                        materialBean1 = new MaterialBean();
                                        materialBean1.setGradeId(pageList.get(i).getGrade());
                                        materialBean1.setGradeName(pageList.get(i).getGradeName());
                                        materialBean1.setIsFree(pageList.get(i).getIsFree());
                                        materialBean1.setPackageIsFree(pageList.get(i).getPackageIsFree());
                                        materialBean1.setVersionId(pageList.get(i).getProductId());
                                        materialBean1.setVersionName(pageList.get(i).getProductName());
                                        materialBean1.setServerId(Integer.valueOf(pageList.get(i).getServiceId()));
                                        materialBean1.setBooksPage(pageList.get(i));
                                        materialBean1.setRequired(pageList.get(i).getRequired());
                                        MaterialBean.BooksBean bean = new MaterialBean.BooksBean();
                                        bean.setBookId(pageList.get(i).getProductId());
                                        bean.setBookName(pageList.get(i).getProductName());
                                        beanList.add(bean);
                                    }
                                }
                                materialBean1.setBooks(beanList);
                                materialBeanList.add(materialBean1);
                            }
                            Set<Integer> integersGrade = mapGrade.keySet();
                            epaperInfoGradeNameBeans.clear();
                            for (Integer gradeId : integersGrade) {
                                EpaperInfoGradeNameBean epaperInfoGradeNameBean = new EpaperInfoGradeNameBean(mapGrade.get(gradeId), gradeId);
                                epaperInfoGradeNameBeans.add(epaperInfoGradeNameBean);
                            }
                            subjectBean.setSubjectId(3);
                            subjectBean.setMaterial(materialBeanList);
                            subjectBeanList.add(subjectBean);
//                            mBookDeskBean.setBookmark(bookmarkBeanList);
                            mBookDeskBean.setSubject(subjectBeanList);
                            getBookMarkAds();
                            //getServiceProductsByTop();
                        }
                    }

                    @Override
                    public void failed(int errorCode, Object data) {
                        //网络获取失败了再走本地获取流程
                        super.failed(errorCode, data);
                        if (!NetworkUtils.isOnline()) {
                            ToastUtil.INSTANCE.toastCenterError(getString(R.string.load_faild_check_net_work));
                            return;
                        }
                    }

                    @Override
                    public void failed(Object data) {
                        super.failed(data);
                        if (!NetworkUtils.isOnline()) {
                            ToastUtil.INSTANCE.toastCenterError(getString(R.string.load_faild_check_net_work));
                            return;
                        }
                    }

                });
    }

    private void showContainerType(ContainerType containerType) {
        if (containerType == ContainerType.TYPE_NO_NET_FAILED) {
            mSwipeToLoadLayout.setVisibility(View.GONE);
            mLlLoadFailed.setVisibility(View.VISIBLE);
            mLlNoData.setVisibility(View.GONE);
        } else if (containerType == ContainerType.TYPE_NO_DATA) {
            mSwipeToLoadLayout.setVisibility(View.GONE);
            mLlNoData.setVisibility(View.VISIBLE);
            mLlLoadFailed.setVisibility(View.GONE);
        } else {
            mSwipeToLoadLayout.setVisibility(View.VISIBLE);
            mLlNoData.setVisibility(View.GONE);
            mLlLoadFailed.setVisibility(View.GONE);
        }
    }

    public void getBookMarkAds() {
        EpaperDao.getInstance().getHeadBookMarkAds(EApplication.BRAND_ID, new BaseCallBack() {
            @Override
            public void failed(int errorCode, Object data) {
                //super.failed(errorCode,data);
                hideCricleProgress();
                mSwipeToLoadLayout.setRefreshing(false);
                if (mBookDeskBean == null) {
                    showContainerType(ContainerType.TYPE_NO_NET_FAILED);
                }
            }

            @Override
            public void failed(Object data) {
                hideCricleProgress();
                mSwipeToLoadLayout.setRefreshing(false);
                if (mBookDeskBean == null) {
                    showContainerType(ContainerType.TYPE_NO_NET_FAILED);
                }
            }

            @Override
            public void success(Object data) {
                hideCricleProgress();
                mSwipeToLoadLayout.setRefreshing(false);
                try {
                    if (data != null) {
                        AdsBean adsBean = (AdsBean) data;
                        mBookDeskBean.setAds(adsBean.getAds());
                        setNetBookMark(adsBean.getBookMarks());
                        mBookDeskBean.setBookmark(bookmarkBeanList);
                        getServiceProductsByTop();
                    }
                } catch (Exception e) {
                }
            }
        });
    }

    private void setNetBookMark(List<BookmarkBean> book) {
        bookmarkBeanList.clear();
        for (int i = 0; i < book.size(); i++) {
            bookmarkBeanList.add(book.get(i));
        }
        BookmarkBean bookmarkBean10 = new BookmarkBean();
        bookmarkBean10.setBookmarkId(12);
        bookmarkBean10.setBookmarkName("看一看");
        bookmarkBean10.setLoaclIcon(R.mipmap.take_a_look_icon);
        bookmarkBeanList.add(bookmarkBean10);
    }

    /**
     * 获取收藏书籍
     */
    public void getServiceProductsByTop() {
        EpaperDao.getInstance().getServiceProductsByTop(EApplication.BRAND_ID, 2
                , 1, 50, 1, 3, "", new BaseExtCallBack(mActivity, EApplication.getInstance().getUserInfoBase().getUserId() + "") {
                    @Override
                    public void failed(int errorCode, Object data) {
                        hideCricleProgress();
                        mSwipeToLoadLayout.setRefreshing(false);
                        mSwipeToLoadLayout.setLoadingMore(false);
                        super.failed(errorCode, data);
                        failed(data);
                        if (!NetworkUtils.isOnline()) {
                            ToastUtil.INSTANCE.toastCenterError(getString(R.string.load_faild_check_net_work));
                            return;
                        }
                    }

                    @Override
                    public void failed(Object data) {
                        hideCricleProgress();
                        mSwipeToLoadLayout.setRefreshing(false);
                        mSwipeToLoadLayout.setLoadingMore(false);
                        if (!NetworkUtils.isOnline()) {
                            ToastUtil.INSTANCE.toastCenterError(getString(R.string.load_faild_check_net_work));
                            return;
                        }
                    }

                    @Override
                    public void success(Object data) {
                        hideCricleProgress();
                        mSwipeToLoadLayout.setRefreshing(false);
                        mSwipeToLoadLayout.setLoadingMore(false);
                        if (null != data) {
                            FavoritesBookPageBean favoritesBookPageBean = (FavoritesBookPageBean) data;
                            List<FavoritesBook> favoritesBookList = favoritesBookPageBean.getPageList();
                            mBookDeskBean.setFavoritesBooks(favoritesBookList);
                            boolean jiaoCaiTongBuAndKeWaiTuoZhanAllEmpty = isJiaoCaiTongBuAndKeWaiTuoZhanAllEmpty(mBookDeskBean.getSubject(), mBookDeskBean.getBookmark(), mBookDeskBean.getFavoritesBooks(), mBookDeskBean.getAds());
                            if (jiaoCaiTongBuAndKeWaiTuoZhanAllEmpty) {
                                showContainerType(ContainerType.TYPE_NO_DATA);
                            } else {
                                showContainerType(ContainerType.TYPE_HAVE_DATA);
                            }
                            mBookDeskRecyclerViewAdapter.updateData(mBookDeskBean, false);
                        }
                    }
                });
    }


    @Override
    public void onRefresh() {
        if (NetworkUtils.isOnline()) {
            getBookDeskHomePageFromNet();
        } else {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    ToastUtil.INSTANCE.toastCenterNoNetError();
                    mSwipeToLoadLayout.setRefreshing(false);
                }
            }, 300);
        }
    }


    @Override
    public void onClick(View v) {
        if (!DoubleClickCheckUtils.vertify500Duration()) {
            return;
        }
        int id = v.getId();
        if (id == R.id.tv_go_to_resource_center) {
            MeJumpManager.jumpToResourseCenterH5Activity(mActivity, R.string.go_back, getResources().getString(R.string.resource_center));
        } else if (id == R.id.btn_retry) {
            getBookDeskHomePageFromNet();
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver();
        EventBus.getDefault().unregister(this);
    }


}